package mbean;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.model.SelectItem;

import util.FacesUtil;
import util.Util;
import vo.GettingStartedSurveyVO;
import vo.QuestionVO;
import dao.SurveyDAO;

public class SurveyMBean extends AbstractMBean{
	
	private List<GettingStartedSurveyVO> listaSurvey;
	private List<SelectItem> listScoresSurvey;
	private SurveyDAO dao;
	private int valorSelecionado;
	private List<QuestionVO> questions;

	public String open() {
		dao = new SurveyDAO();
		this.setListaSurvey(dao.getSurveys(getStrategicPlanId()));
		
		this.listScoresSurvey = new ArrayList<SelectItem>();
		this.listScoresSurvey.add(createSelectItem("1", "1"));
		this.listScoresSurvey.add(createSelectItem("2", "2"));
		this.listScoresSurvey.add(createSelectItem("3", "3"));
		this.listScoresSurvey.add(createSelectItem("4", "4"));
		this.listScoresSurvey.add(createSelectItem("5", "5"));
		this.listScoresSurvey.add(createSelectItem("NA", "0"));
		
		setaTela(false, "");
		
		return "survey";
	}

	public void setListaSurvey(List<GettingStartedSurveyVO> listaSurvey) {
		this.listaSurvey = listaSurvey;
	}

	public List<GettingStartedSurveyVO> getListaSurvey() {
		return listaSurvey;
	}

	public List<SelectItem> getListScoresSurvey() {
		return listScoresSurvey;
	}

	public void setListScoresSurvey(List<SelectItem> listScoresSurvey) {
		this.listScoresSurvey = listScoresSurvey;
	}

	public int getStrategicPlanId() {
		return ((StrategyMBean) FacesUtil.getSessionMapValue("strategy")).getStrategicPlanId();
	}

	private SelectItem createSelectItem(String label, String value) {
		SelectItem si = new SelectItem();
		si.setLabel(label);
		si.setValue(value);
		
		return si;
	}

	public void setValorSelecionado(int valorSelecionado) {
		this.valorSelecionado = valorSelecionado;
	}

	public int getValorSelecionado() {
		return valorSelecionado;
	}
	
	public void save(){
		dao.save(this.listaSurvey);
	}
	
	public void newSurvey(){		
		this.questions = dao.getQuestions();		
	}

	public void setQuestions(List<QuestionVO> questions) {
		this.questions = questions;
	}

	public List<QuestionVO> getQuestions() {
		return questions;
	}

	public void viewSurvey(){
		GettingStartedSurveyVO gssvo = this.getListaSurvey().get(this.valorSelecionado);
		this.questions = gssvo.getListaQuestion();
	}
	
	public void insertSurvey(){
		GettingStartedSurveyVO gssvo = new GettingStartedSurveyVO();
		
		Date data = new Date();
		
		gssvo.setId(-1);
		gssvo.setData(data);
		gssvo.setDataString(Util.convertDateToString(data));
		gssvo.setListaQuestion(this.questions);
		gssvo.setStrategicPlanId(getStrategicPlanId());
		
		int totalScore = 0;
		int numQuestoesRespondidas = 0;
		
		for(QuestionVO qvo:this.questions){
			totalScore += qvo.getScore();
			
			if (qvo.getScore() > 0){
				numQuestoesRespondidas++;
			}
		}
		
		float score;
		
		if (numQuestoesRespondidas == 0)
			score = 0;
		else
			score = ((totalScore / numQuestoesRespondidas) * 10) / 5;
		
		gssvo.setScore(score);
		
		this.listaSurvey.add(gssvo);
		dao.save(this.listaSurvey);
		
		MenuMBean mmb = (MenuMBean) FacesUtil.getSessionMapValue("menu");
		mmb.loadTree();
	}

	@Override
	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}
}
